﻿@{
    ViewBag.Title = "Đăng nhập";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">
    Ext.onReady(function () {

        Ext.create('Ext.container.Viewport', {
            layout: 'fit',
            items: [{
                xtype: 'window',
                title: 'Đăng nhập',
                closable: false,
                resizable: false,
                modal: true,
                autoShow: true,
                layout: 'fit',
                icon: '../Content/icon/lock.png',
                width: 350,
                height: 160,
                items: [{
                    xtype: 'form',
                    layout: 'form',
                    id: 'formMain',
                    bodyPadding: '10 10 10 10',
                    bodyStyle: {
                        background: '#E0E0E0'
                    },
                    fieldDefaults: {
                        msgTarget: 'side',
                        labelWidth: 90
                    },
                    defaultType: 'textfield',
                    items: [{
                        fieldLabel: 'Username',
                        id: 'txtUsername',
                        allowBlank: false
                    }, {
                        inputType: 'password',
                        fieldLabel: 'Password',
                        id: 'txtPassword',
                        allowBlank: false
                    }, {
                        xtype: 'label',
                        id: 'lblInfo',
                        cls: 'color: red',
                        text: ''
                    }]
                }],
                buttons: [{
                    text: 'Đăng nhập',
                    id: 'btnlogin',
                    icon: '../Content/icon/accept.png',
                    handler: function () {
                        var entity = {
                            Username: Ext.getCmp('txtUsername').getValue(),
                            Password: Ext.getCmp('txtPassword').getValue()
                        };
                        Ext.Ajax.request({
                            url: '/api/ApiLogin/Logon',
                            method: 'POST',
                            jsonData: entity,
                            success: function (transport, option) {
                                var responseData = Ext.decode(transport.responseText);
                                if (responseData.success == true) {
                                    if (responseData.login == true) {
                                        var getParams = document.URL.split('?');
                                        if (getParams.length > 1) {
                                            var params = Ext.urlDecode(getParams[1]);
                                            var a = params.ReturnUrl;
                                            if (a) {
                                                window.location = a;
                                            } else {
                                                window.location = '/';
                                            }
                                        }
                                        else {
                                            window.location = '/';
                                        }
                                        
                                    }
                                    else {
                                        Ext.getCmp('lblInfo').setText('Sai mật khẩu hoặc tên đăng nhập. Mời nhập lại');
                                        Ext.getCmp('txtUsername').focus(true, 200);
                                        Ext.getCmp('txtPassword').setValue('');
                                    }
                                } else {
                                    Ext.MessageBox.show({
                                        title: 'Lỗi',
                                        msg: responseData.message,
                                        icon: Ext.MessageBox.ERROR,
                                        buttons: Ext.Msg.OK
                                    });
                                }
                            },
                            failure: function (transport, option) {
                                var responseData = Ext.decode(transport.responseText);
                                Ext.MessageBox.show({
                                    title: 'Lỗi',
                                    msg: responseData.message,
                                    icon: Ext.MessageBox.ERROR,
                                    buttons: Ext.Msg.OK
                                });
                            }
                        });
                    }
                }, {
                    text: 'Thoát',
                    id: 'btnCancel',
                    icon: '../Content/icon/decline.png',
                    handler: function () {
                        Ext.Ajax.request({
                            url: '/api/ApiLogin/LogOff',
                            method: 'POST',
                            success: function (transport, option) {
                                var responseData = Ext.decode(transport.responseText);
                                if (responseData.success == true) {
                                    
                                } else {
                                    Ext.MessageBox.show({
                                        title: 'Lỗi',
                                        msg: responseData.message,
                                        icon: Ext.MessageBox.ERROR,
                                        buttons: Ext.Msg.OK
                                    });
                                }
                            },
                            failure: function (transport, option) {
                                var responseData = Ext.decode(transport.responseText);
                                Ext.MessageBox.show({
                                    title: 'Lỗi',
                                    msg: responseData.message,
                                    icon: Ext.MessageBox.ERROR,
                                    buttons: Ext.Msg.OK
                                });
                            }
                        });
                    }
                }]
            }]
        });
    });
</script>
